home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Why does this work?
- Date: 25 Feb 1996 12:19:04 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqg7oINN26a@keats.ugrad.cs.ubc.ca>
- References: <4g8f7o$adt@ncar.ucar.edu> <4ggsi6$1fg@dopey.magg.net>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4ggsi6$1fg@dopey.magg.net>, Dennis Hawkins <n4mwd@magg.net> wrote:
- >rosinski@ra.cgd.ucar.edu (Jim Rosinski) wrote:
- >
- >>Could someone please explain why the following code works? In particular, I
- >>am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
- >
- >>#include <stdio.h>
- >>main()
- >>{
- >> void sub1(), sub2();
- >
- >> struct cmndstruct {
- >> void (*funcnam)();
- >> };
- >
- >> struct cmndstruct cmndtable[] = {
- >> sub1,
- >> sub2,
- >> NULL
- >> };
- >> struct cmndstruct *cmndptr;
- >
- >> for (cmndptr = cmndtable; *(cmndptr->funcnam) != NULL; cmndptr++) {
- >> (*(cmndptr->funcnam))();
- >> }
- >> exit(0);
- >>}
- >
- >>void sub1()
- >>{
- >> printf("Inside sub1\n");
- >>}
- >
- >>void sub2()
- >>{
- >> printf("Inside sub2\n");
- >>}
- >
- >There is nothing really all that unusual about your program. The only
- >thing that looks a little strange to me was that you have a structure
- >with only one field in it. A simple array of pointers to functions
-
- He probably did that to underscore the fact that apparently functions are being
- assigned to structure types. You need only a single-membered structure to
- indicate that.
-
- >would be more efficient. As far as the machine knows, that is
- >precisely what you have here.
-
- Maybe not. The alignment constraints for a structure could be different from
- those for bare function pointers. Though this is not likely in practice.
- --
-
-